package com.ibiaoke.boss.service;

import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.consts.PayTypeConst;
import com.ibiaoke.customer.dao.DepositDao;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.customer.service.DepositRecorderService;
import com.ibiaoke.customer.service.DepositService;
import com.ibiaoke.marketing.service.CouponService;
import com.ibiaoke.order.consts.OrderStatusConst;
import com.ibiaoke.order.dao.XcxOrderDao;
import com.ibiaoke.order.entity.Order;
import com.ibiaoke.order.entity.XcxOrder;
import com.ibiaoke.order.service.OrderPayService;
import com.ibiaoke.order.service.OrderService;
import com.ibiaoke.order.service.XcxPayOrderService;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService{

    //预存款支付方式
    public Integer DEPOSIT_PAY = 3;
    //预存款消费类型
    public Integer DEPOSIT_PAY_TYPE = 2;

    @Autowired
    private ShopDao shopDao;

    @Autowired
    private DepositDao depositDao;

    @Autowired
    private CouponService couponService;

    @Autowired
    private DepositService depositService;

    @Autowired
    private DepositRecorderService depositRecorderService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderPayService orderPayService;

    @Autowired
    private XcxOrderDao xcxOrderDao;

    @Autowired
    private XcxPayOrderService xcxPayOrderService;



    @Override
    public Map<String, String> offLinePay(Integer shopId, BigDecimal cash, Customer customer, String openId) {
        AssertUtils.notNull(shopId, "请选择店铺");
        AssertUtils.notNull(cash, "请输入金额");
        Shop shop = shopDao.selectByPrimaryKey(shopId);
        AssertUtils.notNull(shop,"店铺不存在");
        AssertUtils.requireTrue(cash.compareTo(new BigDecimal(10000000))<0,"请输入实际金额");

        Order order = new Order();
        order.setShopId(shopId);                            //店铺编号
        order.setDeliveryType(1);                           //订单类型：自提
        order.setUserId(customer.getCustomerId());                            //用户编号
        order.setOrderPrice(cash);      //订单金额
        order.setDeliveryAddress(shop.getShopAddress());    //店铺地址
        order.setExpressPrice(BigDecimal.valueOf(0));       //订单运费

        //订单经纬度(店铺经纬度)
        order.setLongitude(shop.getDesLongitude());
        order.setLatitude(shop.getDesLatitude());

        order.setCreateTime(new Date());                    //订单创建时间
        order.setUpdateTime(new Date());                    //订单修改时间
        order.setSource("WECHAT");                          //订单来源
        order.setContactMobile(customer.getCustomerMobile());
        order.setContactPersion(customer.getCustomerNickname());
        order.setOrderStatus(OrderStatusConst.UNPAID);

        String orderNo = orderService.generateOrderNo();    //10位系统时间戳+4位的随机数作为订单号
        order.setOrderCode(orderNo);                        //订单编号

        orderService.saveOrder(order);                      //创建新的订单

        Order orderInfo = orderService.getOrderInfoByCode(orderNo);//获取订单Id

        return orderPayService.payOrderByWeChat(orderInfo.getOrderId(), openId);       //订单支付
    }

    @Override
    public String offLinePayByAli(Integer shopId, BigDecimal cash, Customer customer) {
        AssertUtils.notNull(shopId, "请选择店铺");
        AssertUtils.notNull(cash, "请输入金额");
        Shop shop = shopDao.selectByPrimaryKey(shopId);
        AssertUtils.notNull(shop,"店铺不存在");
        AssertUtils.requireTrue(cash.compareTo(new BigDecimal(10000000))<0,"请输入实际金额");

        Order order = new Order();
        order.setShopId(shopId);                            //店铺编号
        order.setDeliveryType(1);                           //订单类型：自提
        order.setUserId(customer.getCustomerId());                            //用户编号
        order.setOrderPrice(cash);      //订单金额
        order.setDeliveryAddress(shop.getShopAddress());    //店铺地址
        order.setExpressPrice(BigDecimal.valueOf(0));       //订单运费

        //订单经纬度(店铺经纬度)
        order.setLongitude(shop.getDesLongitude());
        order.setLatitude(shop.getDesLatitude());

        order.setCreateTime(new Date());                    //订单创建时间
        order.setUpdateTime(new Date());                    //订单修改时间
        order.setSource("ALIPAY");                          //订单来源
        order.setContactMobile(customer.getCustomerMobile());
        order.setContactPersion(customer.getCustomerNickname());
        order.setOrderStatus(OrderStatusConst.UNPAID);

        String orderNo = orderService.generateOrderNo();    //10位系统时间戳+4位的随机数作为订单号
        order.setOrderCode(orderNo);                        //订单编号

        orderService.saveOrder(order);                      //创建新的订单

        Order orderInfo = orderService.getOrderInfoByCode(orderNo);//获取订单Id

        return orderPayService.payOrderByAliPay(orderInfo.getOrderId());       //订单支付
    }

    @Override
    public Map<String, String> xcxOffLinePay(Integer shopId, BigDecimal cash, Customer customer, String openId) {
        AssertUtils.notNull(shopId, "请选择店铺");
        AssertUtils.notNull(cash, "请输入金额");
        Shop shop = shopDao.selectByPrimaryKey(shopId);
        AssertUtils.notNull(shop,"店铺不存在");
        AssertUtils.requireTrue(cash.compareTo(new BigDecimal(10000000))<0,"请输入实际金额");

        XcxOrder order = new XcxOrder();
        order.setShopId(shopId);                            //店铺编号
        order.setDeliveryType(1);                           //订单类型：自提
        order.setUserId(customer.getCustomerId());                            //用户编号
        order.setOrderPrice(cash);      //订单金额
        order.setDeliveryAddress(shop.getShopAddress());    //店铺地址
        order.setExpressPrice(BigDecimal.valueOf(0));       //订单运费
        order.setOrderType(new Byte("3"));                //快捷支付
        order.setPayType(1);
        //订单经纬度(店铺经纬度)
        order.setLongitude(shop.getDesLongitude());
        order.setLatitude(shop.getDesLatitude());

        order.setCreateTime(new Date());                    //订单创建时间
        order.setUpdateTime(new Date());                    //订单修改时间
        order.setEndTime(new Date());
        order.setSource("小程序快捷支付");                          //订单来源
        order.setContactMobile(customer.getCustomerMobile());
        order.setContactPersion(customer.getCustomerNickname());
        order.setOrderStatus(OrderStatusConst.UNPAID);
        order.setDelStatus(new Byte("0"));

        String orderNo = orderService.generateOrderNo();    //10位系统时间戳+4位的随机数作为订单号
        order.setOrderCode(orderNo);                        //订单编号

        xcxOrderDao.saveXcxOrder(order);                      //创建新的订单

        XcxOrder orderInfo = xcxOrderDao.getOrderByOrderCode(order.getOrderCode());//获取订单Id

        return xcxPayOrderService.payXcxOrderByWe(orderInfo.getOrderId(), openId);       //订单支付
    }
}
